package com.remote.server.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.remote.server.feigin.OrderServiceFeigin;
import com.smartcar.common.domain.User;
import com.smartcar.common.domain.request.OrderRequest;
import com.smartcar.common.domain.response.OrderResponse;
import com.smartcar.common.result.PageResult;
import com.smartcar.common.result.Result;
import com.remote.common.OrderReceiving;
import com.remote.server.service.DispatchService;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * 车辆调度系统
 * PostConstruct 初始化
 */
@Log4j2
@RestController
@RequestMapping("/dispatch")
public class DispatchController {

    @Autowired
    private DispatchService dispatchService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private OrderServiceFeigin orderServiceFeigin;


    /**
     * 查询未接单司机
     * @param
     * @return
     */

    @PostMapping("/dispatch")
    public Result<PageResult<User>> missedOrder(){
        List<User> users = dispatchService.missedOrder();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return PageResult.toResult(pageInfo.getTotal(),users);
    }

    /**
     * 查询已下单用户
     * @param
     * @return
     */

//    @PostMapping("/userOrder")
//    public Result<PageResult<OrderResponse>> userOrder(){
//        List<OrderResponse> list = dispatchService.userOrder();
//        PageInfo<OrderResponse> pageInfo = new PageInfo<>(list);
//        return PageResult.toResult(pageInfo.getTotal(),list);
//    }

    /**
     * 派单
     */

    @PostMapping("/melody")
    public Result melody(@RequestBody OrderReceiving orderReceiving){
//        dispatchService.melody(orderReceiving);
        return Result.success();
    }

    @PostMapping("/order")
    public Result<PageResult<OrderReceiving>> order(@RequestBody OrderReceiving orderReceiving){
        PageHelper.startPage(orderReceiving.getPageNum(),orderReceiving.getPageSize());
//        List<OrderReceiving> list = dispatchService.ReceivingList(orderReceiving);
//        PageInfo<OrderReceiving> pageInfo = new PageInfo<>(list);
//        return PageResult.toResult(pageInfo.getTotal(),list);
        return Result.success();
    }

    @PostMapping("/start")
    public Result Start(){



        List<String> driver = redisTemplate.opsForList().range("driver", 0, -1);
//        List<String> userOrder = redisTemplate.opsForList().range("userOrder", 0, -1);


//        for (String s : userOrder) {
//            Order order = JSONObject.parseObject(s, Order.class);
//            OrderReceiving build = OrderReceiving
//                    .builder()
//                    .orderStarting(order.getOrderStarting())
//                    .orderDestination(order.getOrderDestination())
//                    .build();
//            String s1 = driver.stream().filter(t ->
//               JSONObject.parseObject(t, User.class).getStatus()==1).findAny().get();
//            System.out.println(order);
//            dispatchService.melody(build);
//        }
//
//        for (String s : driver) {
//            System.out.println(s);
//        }




//        List<String> user = redisTemplate.opsForList().range("user", 0, -1);
//        for (String s : user) {
//            User user1 = JSONObject.parseObject(s, User.class);
//            System.out.println(user1);
//        }


        List<User> users = dispatchService.missedOrder();


        return Result.success();

    }

    @PostMapping("/down")
    public Result down(){
//        Result<ArrayList<OrderResponse>> list = orderServiceFeigin.orderList();
//        return Result.success(list);

        List<String> userOrder = redisTemplate.opsForList().range("userOrder", 0, -1);
        List<String> driver = redisTemplate.opsForList().range("driver", 0, -1);
        List<Object> strings = new ArrayList<>();



//        for (String s : userOrder) {
//
//            OrderReceiving orderReceiving = JSONObject.parseObject(s, OrderReceiving.class);
//
////            OrderReceiving build = OrderReceiving.builder()
////                    .orderStarting(orderReceiving.getOrderStarting())
////                    .orderDestination(orderReceiving.getOrderDestination())
////                    .build();
//
//
//        }
//
//        for (String s : driver) {
//            OrderReceiving orderReceiving = JSONObject.parseObject(s, OrderReceiving.class);
//
//        }


        Result<ArrayList<OrderResponse>> arrayListResult = orderServiceFeigin.orderList();
        System.out.println(arrayListResult.getData());
//        dispatchService.ReceivingList(build);
        return Result.success();
    }

    @PostMapping("/selectDriver")
    public Result selectDriver() {
        List<String> driver = redisTemplate.opsForList().range("driver", 0, -1);

        List<User> collect = driver.stream().map(s -> JSONObject.parseObject(s, User.class)).collect(Collectors.toList());

        List<User> list = new ArrayList<>();

        for (User user : collect) {
            list.add(user);
        }

        log.info("司机信息已取出");
        return Result.success(list);
    }

    /**
     * 用户下单列表
     * @return
     */

    @RequestMapping("/sendOrder")
    public Result SendOrder() {
        List<String> order = redisTemplate.opsForList().range("order", 0, -1);

        List<OrderRequest> collect = order.stream().map(t -> JSON.parseObject(t, OrderRequest.class)).collect(Collectors.toList());

        List<OrderRequest> list = new ArrayList<>();

        for (OrderRequest orderRequest : collect) {
            list.add(orderRequest);
        }

        return Result.success(list);

    }

    /**
     * 派单
     * @return
     */
    @RequestMapping("/sendOrders")
    public Result SendPcOrder(@RequestBody OrderRequest orderRequest){
                OrderRequest byIdOrder = orderServiceFeigin.findByIdOrder(orderRequest.getId());


        OrderRequest orderRequest1 = new OrderRequest();
        orderRequest1.setDriverName(byIdOrder.getDriverName());
        orderRequest1.setDriverPhone(byIdOrder.getDriverPhone());
        orderRequest1.setDriverCarcode(byIdOrder.getDriverCarcode());

        orderServiceFeigin.updatePc(orderRequest1);
        return Result.success();
    }

    /**
     * 从redis表里找到符合条件的数据
     *
     * @param id
     * @return
     */

    @RequestMapping("/findById/{id}")
    public CompletableFuture<Result<List<OrderRequest>>> findById(@PathVariable("id") Long id){
      return CompletableFuture.supplyAsync(() -> {
            List<String> order = redisTemplate.opsForList().range("order", 0, -1);
            List<OrderRequest> list = order.stream()
                    .map(s -> JSONObject.parseObject(s, OrderRequest.class))
                    .filter(orderRequest -> orderRequest.getId().equals(id))
                    .collect(Collectors.toList());
          return Result.success(list);
        });




    }

}
